import pandas as pd
import json

data=pd.read_csv('shop_info.csv',index_col=False)

#   设置参数让dataframe完整显示
pd.set_option('display.max_rows',None)          #   不限制行数
pd.set_option('display.max_columns',None)       #   不限制列数
pd.set_option('display.width',None)             #   自动调整宽度
pd.set_option('display.max_colwidth',None)      #   不限制列内容的宽度



#   拿到商品种类名称
class_name_list=data['class_name'].drop_duplicates().tolist()

result_dict={}

for x in class_name_list:
    # print(x)
    dict_list=[]
    for row in data.itertuples():
        str_row = str(row).strip('Pandas(').strip(')')
        list_row = str_row.split(",")
        class_name = list_row[2].split("=")[1].strip("'")
        # print(class_name)
        # print(list_row)
        if class_name==x:
            dict_list.append(
                {
                    "title":list_row[4].split("=")[1].strip("'"),
                    "price":list_row[5].split("=")[1].strip("'"),
                    "address":list_row[6].split("=")[1].strip("'"),
                    "buy_number":list_row[7].split("=")[1].strip("'"),
                    "shop_name":list_row[8].split("=")[1].strip("'"),
                    "image_url":list_row[9].split("=")[1].strip("'"),
                    "info_url":list_row[10].split("=")[1].strip("'"),
                    "rating":list_row[11].split("=")[1].strip("'")
                }
            )
    result_dict.update(
        {f"class_name(eg:{x})":dict_list}
    )



for j in result_dict.keys():
    print(j)

with open('shop_info.json','w',encoding='utf-8') as jsonfile:
    #   将字典写入json文件
    json.dump(result_dict,jsonfile,ensure_ascii=False,indent=4)


